package cn.edu.scau.cmi.ema.controller.rest;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.io.IOException;

import com.google.gson.Gson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiPsonService;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;

import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.WebDataBinder;

@Controller("AddressRestController")
public class AddressRestController extends CmiEmaController {

	/**
	 * Create a new Address entity
	 * 
	 */
	@RequestMapping(value = "/newAddress", method = RequestMethod.POST)
	@ResponseBody
	public Object newAddress(@RequestBody Address address) {
		addressService.saveAddress(address);
		return JSONObject.parse(cmiPsonService.toJsonString(addressDAO.findAddressByPrimaryKey(address.getId()), 2));
	}

	@RequestMapping(value = "/restNewAddress", method = RequestMethod.POST)
	@ResponseBody
	public Object restNewAddress(@RequestBody String addressJson) {
		Address address = new Gson().fromJson(addressJson, Address.class);
		appendReferences(address);
		addressService.saveAddress(address);
		return JSONObject.parse(cmiPsonService.toJsonString(addressDAO.findAddressByPrimaryKey(address.getId()), 2));
	}

	@RequestMapping(value = "/saveAddress", method = RequestMethod.PUT)
	@ResponseBody
	public Address saveAddress(@RequestBody Address address) {
		addressService.saveAddress(address);
		return addressDAO.findAddressByPrimaryKey(address.getId());
	}

	@RequestMapping(value = "/restSaveAddress", method = RequestMethod.PUT)
	@ResponseBody
	public Object restSaveAddress(@RequestBody String addressJson) {
		Address address = new Gson().fromJson(addressJson, Address.class);
		appendReferences(address);
		CmiPsonService cmiPsonService = new CmiPsonService();
		addressService.saveAddress(address);
		return JSONObject.parse(cmiPsonService.toJsonString(addressDAO.findAddressByPrimaryKey(address.getId()), 1));
	}

	@RequestMapping(value = "/restDeleteAddress/{addressId}", method = RequestMethod.DELETE)
	@ResponseBody
	public void restDeleteAddress(@PathVariable Integer addressId) {
		Address address = addressDAO.findAddressByPrimaryKey(addressId);
		addressService.deleteAddress(address);
	}

	@RequestMapping(value = "/restConfirmDeleteAddresss")
	@ResponseBody
	public Object restConfirmDeleteAddresss(@RequestBody Set<Integer> deleteAddresssIds) {
		Set<Address> deleteAddresss = addressDAO.findAddresssByIds(deleteAddresssIds);
		Set<Address> referencedAddresss = addressService.loadReferenceAddresss(deleteAddresss);
		deleteAddresss.removeAll(referencedAddresss);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("referencedEntitys", referencedAddresss);
		jsonObject.put("deleteEntitys", deleteAddresss);
		return JSONObject.parse(jsonObject.toJSONString());
	}

	@RequestMapping("/restDeleteAddresss")
	@ResponseBody
	public Object restDeleteAddresss(@RequestBody Set<Address> deleteAddresss) {
		Iterator<Address> iterator = deleteAddresss.iterator();
		while (iterator.hasNext()) {
			Address currentAddress = iterator.next();
			addressService.deleteAddress(currentAddress);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("url", "listAddresss");
		return JSONObject.parse(jsonObject.toJSONString());
	}
	// loadself

	@RequestMapping(value = "/restLoadAddress/{addressId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddress(@PathVariable Integer addressId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(addressDAO.findAddressByPrimaryKey(addressId), 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadAddressAddress/{addressId}/address", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressAddress(@PathVariable Integer addressId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(
				cmiPsonService.toPsonString(addressDAO.findAddressByPrimaryKey(addressId).getParent(), 2, cmiPagination));
	}
	// loadReferenceds
	// TODO loadReferencedsPson，这个方法有一些问题，待修复

	@RequestMapping(value = "/restLoadAddressAdditiveaddresss/{addressId}/additiveaddresss/{relatedadditiveaddressId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressAdditiveaddresss(@PathVariable Integer addressId,
			@PathVariable Integer relatedadditiveaddressId, @RequestBody CmiPagination cmiPagination) {
		Additiveaddress additiveaddress = additiveaddressDAO.findAdditiveaddressByPrimaryKey(relatedadditiveaddressId,
				-1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(additiveaddress, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadAddressAddresss/{addressId}/addresss/{relatedaddressId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressAddresss(@PathVariable Integer addressId, @PathVariable Integer relatedaddressId,
			@RequestBody CmiPagination cmiPagination) {
		Address address = addressDAO.findAddressByPrimaryKey(relatedaddressId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(address, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadAddressDetectdepartments/{addressId}/detectdepartments/{relateddetectdepartmentId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressDetectdepartments(@PathVariable Integer addressId,
			@PathVariable Integer relateddetectdepartmentId, @RequestBody CmiPagination cmiPagination) {
		Detectdepartment detectdepartment = detectdepartmentDAO
				.findDetectdepartmentByPrimaryKey(relateddetectdepartmentId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(detectdepartment, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadAddressEventsForSourceAddress/{addressId}/eventsForSourceAddress/{relatedeventId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressEventsForSourceAddress(@PathVariable Integer addressId,
			@PathVariable Integer relatedeventId, @RequestBody CmiPagination cmiPagination) {
		Event event = eventDAO.findEventByPrimaryKey(relatedeventId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(event, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadAddressEventsForDiscoveryAddress/{addressId}/eventsForDiscoveryAddress/{relatedeventId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressEventsForDiscoveryAddress(@PathVariable Integer addressId,
			@PathVariable Integer relatedeventId, @RequestBody CmiPagination cmiPagination) {
		Event event = eventDAO.findEventByPrimaryKey(relatedeventId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(event, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadAddressFoodsaleaddresss/{addressId}/foodsaleaddresss/{relatedfoodsaleaddressId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressFoodsaleaddresss(@PathVariable Integer addressId,
			@PathVariable Integer relatedfoodsaleaddressId, @RequestBody CmiPagination cmiPagination) {
		Foodsaleaddress foodsaleaddress = foodsaleaddressDAO.findFoodsaleaddressByPrimaryKey(relatedfoodsaleaddressId,
				-1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(foodsaleaddress, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadAddressMaterialaddresss/{addressId}/materialaddresss/{relatedmaterialaddressId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressMaterialaddresss(@PathVariable Integer addressId,
			@PathVariable Integer relatedmaterialaddressId, @RequestBody CmiPagination cmiPagination) {
		Materialaddress materialaddress = materialaddressDAO.findMaterialaddressByPrimaryKey(relatedmaterialaddressId,
				-1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(materialaddress, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadAddressProducers/{addressId}/producers/{relatedproducerId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressProducers(@PathVariable Integer addressId, @PathVariable Integer relatedproducerId,
			@RequestBody CmiPagination cmiPagination) {
		Producer producer = producerDAO.findProducerByPrimaryKey(relatedproducerId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(producer, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadAddressSuppliers/{addressId}/suppliers/{relatedsupplierId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadAddressSuppliers(@PathVariable Integer addressId, @PathVariable Integer relatedsupplierId,
			@RequestBody CmiPagination cmiPagination) {
		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(relatedsupplierId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(supplier, 2, cmiPagination));
	}

	public void appendReferences(Address address) {

		Address parent = address.getParent();
		if (parent != null)
			address.setParent(addressService.findAddressByPrimaryKey(parent.getId()));

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Additiveaddress> additiveaddresssForAddress = new HashSet<>();
		for (Additiveaddress additiveaddress_Address : address.getAdditiveaddresssForAddress()) {
			additiveaddresssForAddress
					.add(additiveaddressService.findAdditiveaddressByPrimaryKey(additiveaddress_Address.getId()));
		}
		address.setAdditiveaddresssForAddress(additiveaddresssForAddress);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Address> addresssForParent = new HashSet<>();
		for (Address address_Parent : address.getAddresssForParent()) {
			addresssForParent.add(addressService.findAddressByPrimaryKey(address_Parent.getId()));
		}
		address.setAddresssForParent(addresssForParent);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Detectdepartment> detectdepartmentsForAddress = new HashSet<>();
		for (Detectdepartment detectdepartment_Address : address.getDetectdepartmentsForAddress()) {
			detectdepartmentsForAddress
					.add(detectdepartmentService.findDetectdepartmentByPrimaryKey(detectdepartment_Address.getId()));
		}
		address.setDetectdepartmentsForAddress(detectdepartmentsForAddress);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Event> eventsForSourceAddress = new HashSet<>();
		for (Event event_SourceAddress : address.getEventsForSourceAddress()) {
			eventsForSourceAddress.add(eventService.findEventByPrimaryKey(event_SourceAddress.getId()));
		}
		address.setEventsForSourceAddress(eventsForSourceAddress);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Event> eventsForDiscoveryAddress = new HashSet<>();
		for (Event event_DiscoveryAddress : address.getEventsForDiscoveryAddress()) {
			eventsForDiscoveryAddress.add(eventService.findEventByPrimaryKey(event_DiscoveryAddress.getId()));
		}
		address.setEventsForDiscoveryAddress(eventsForDiscoveryAddress);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Foodsaleaddress> foodsaleaddresssForAddress = new HashSet<>();
		for (Foodsaleaddress foodsaleaddress_Address : address.getFoodsaleaddresssForAddress()) {
			foodsaleaddresssForAddress
					.add(foodsaleaddressService.findFoodsaleaddressByPrimaryKey(foodsaleaddress_Address.getId()));
		}
		address.setFoodsaleaddresssForAddress(foodsaleaddresssForAddress);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Materialaddress> materialaddresssForAddress = new HashSet<>();
		for (Materialaddress materialaddress_Address : address.getMaterialaddresssForAddress()) {
			materialaddresssForAddress
					.add(materialaddressService.findMaterialaddressByPrimaryKey(materialaddress_Address.getId()));
		}
		address.setMaterialaddresssForAddress(materialaddresssForAddress);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Producer> producersForAddress = new HashSet<>();
		for (Producer producer_Address : address.getProducersForAddress()) {
			producersForAddress.add(producerService.findProducerByPrimaryKey(producer_Address.getId()));
		}
		address.setProducersForAddress(producersForAddress);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Supplier> suppliersForAddress = new HashSet<>();
		for (Supplier supplier_Address : address.getSuppliersForAddress()) {
			suppliersForAddress.add(supplierService.findSupplierByPrimaryKey(supplier_Address.getId()));
		}
		address.setSuppliersForAddress(suppliersForAddress);
	}

	@RequestMapping(value = "/restGetAddressAddress/{addressId}/address", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressAddress(@PathVariable Integer addressId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(
				cmiPsonService.toPsonString(addressDAO.findAddressByPrimaryKey(addressId).getParent(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetAddressAdditiveaddresss/{addressId}/additiveaddresss", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressAdditiveaddresss(@PathVariable Integer addressId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				addressDAO.findAddressByPrimaryKey(addressId).getAdditiveaddresssForAddress(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetAddressAddresss/{addressId}/addresss", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressAddresss(@PathVariable Integer addressId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService
				.toPsonString(addressDAO.findAddressByPrimaryKey(addressId).getAddresssForParent(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetAddressDetectdepartments/{addressId}/detectdepartments", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressDetectdepartments(@PathVariable Integer addressId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				addressDAO.findAddressByPrimaryKey(addressId).getDetectdepartmentsForAddress(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetAddressEventsForSourceAddress/{addressId}/eventsForSourceAddress", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressEventsForSourceAddress(@PathVariable Integer addressId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService
				.toPsonString(addressDAO.findAddressByPrimaryKey(addressId).getEventsForSourceAddress(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetAddressEventsForDiscoveryAddress/{addressId}/eventsForDiscoveryAddress", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressEventsForDiscoveryAddress(@PathVariable Integer addressId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				addressDAO.findAddressByPrimaryKey(addressId).getEventsForDiscoveryAddress(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetAddressFoodsaleaddresss/{addressId}/foodsaleaddresss", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressFoodsaleaddresss(@PathVariable Integer addressId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				addressDAO.findAddressByPrimaryKey(addressId).getFoodsaleaddresssForAddress(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetAddressMaterialaddresss/{addressId}/materialaddresss", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressMaterialaddresss(@PathVariable Integer addressId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				addressDAO.findAddressByPrimaryKey(addressId).getMaterialaddresssForAddress(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetAddressProducers/{addressId}/producers", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressProducers(@PathVariable Integer addressId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService
				.toPsonString(addressDAO.findAddressByPrimaryKey(addressId).getProducersForAddress(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetAddressSuppliers/{addressId}/suppliers", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetAddressSuppliers(@PathVariable Integer addressId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService
				.toPsonString(addressDAO.findAddressByPrimaryKey(addressId).getSuppliersForAddress(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restListAddresss", method = RequestMethod.GET)
	@ResponseBody
	public Object restListAddresss() {
		return JSONObject.parse(cmiPsonService.toJsonString(addressService.loadAddresss(), 2));
	}

	@RequestMapping(value = "/restListAddresssPost", method = RequestMethod.POST)
	@ResponseBody
	public Object restListAddresssPost(@RequestBody CmiPagination cmiPagination) {

		CmiPsonService cmiPsonService = new CmiPsonService();
		cmiPagination = addressPaginationService.updateAppPagination(cmiPagination);
		int startResult = (cmiPagination.getCurrentPagination() - 1) * cmiPagination.getPaginationSize();

		Set<Address> addresss = addressDAO.findAllAddresss(startResult, cmiPagination.getPaginationSize());
		String addressString = cmiPsonService.toPsonString(addresss, 2, cmiPagination);

		return JSONObject.parse(addressString);
	}

	@RequestMapping("/addressSelectBox")
	@ResponseBody
	public JSONObject addressSelectBox(@RequestParam(required = false) String searchTable,
			@RequestParam(required = false) Set<Integer> searchValue, // 杨宏宇2019-9-10：searchValue是在selectpage.js的616行定义，如果要修改searchValue的名称，1.可以修改在selectpage.js修改；2.可以
																		// @RequestParam(value="searchValue", required =
																		// false) Set<Integer> someName
			@RequestParam(required = false, defaultValue = "10") int paginationSize,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
			@RequestParam(value = "qWord[]", required = false) String queryword,
			@RequestParam(required = false) String selectItem) throws IOException {
		Set entitySet = null;
		Integer count = 0;

		// 初始化原始值

		if (searchValue != null) {
			switch (searchTable) {
			// 外键

			// 被引用键
			case "Additiveaddress":
				entitySet = additiveaddressService.loadAdditiveaddresssByIdSet(searchValue);
				break;
			case "Address":
				entitySet = addressService.loadAddresssByIdSet(searchValue);
				break;
			case "Detectdepartment":
				entitySet = detectdepartmentService.loadDetectdepartmentsByIdSet(searchValue);
				break;
			case "Event":
				entitySet = eventService.loadEventsByIdSet(searchValue);
				break;
			case "Foodsaleaddress":
				entitySet = foodsaleaddressService.loadFoodsaleaddresssByIdSet(searchValue);
				break;
			case "Materialaddress":
				entitySet = materialaddressService.loadMaterialaddresssByIdSet(searchValue);
				break;
			case "Producer":
				entitySet = producerService.loadProducersByIdSet(searchValue);
				break;
			case "Supplier":
				entitySet = supplierService.loadSuppliersByIdSet(searchValue);
				break;
			}
			// 没有测试，深度需要多少？？？
			String jsonData = cmiPsonService.toJsonString(entitySet, 1);
			JSONArray jsonArray = new JSONArray();
			jsonArray = (JSONArray) JSON.parse((String) jsonData);

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("list", jsonArray);
			jsonObject.put("totalRow", entitySet.size());
			return jsonObject;
		}

		// 不根据关键字进行搜索
		if (queryword == null || queryword.trim() == "") {
			switch (selectItem) {
			// 外键

			// 被引用键
			case "Additiveaddress":
				entitySet = additiveaddressDAO.findAllAdditiveaddresss((pageNumber - 1) * paginationSize,
						paginationSize);
				count = additiveaddressService.countAdditiveaddresss();
				break;
			case "Address":
				entitySet = addressDAO.findAllAddresss((pageNumber - 1) * paginationSize, paginationSize);
				count = addressService.countAddresss();
				break;
			case "Detectdepartment":
				entitySet = detectdepartmentDAO.findAllDetectdepartments((pageNumber - 1) * paginationSize,
						paginationSize);
				count = detectdepartmentService.countDetectdepartments();
				break;
			case "Event":
				entitySet = eventDAO.findAllEvents((pageNumber - 1) * paginationSize, paginationSize);
				count = eventService.countEvents();
				break;
			case "Foodsaleaddress":
				entitySet = foodsaleaddressDAO.findAllFoodsaleaddresss((pageNumber - 1) * paginationSize,
						paginationSize);
				count = foodsaleaddressService.countFoodsaleaddresss();
				break;
			case "Materialaddress":
				entitySet = materialaddressDAO.findAllMaterialaddresss((pageNumber - 1) * paginationSize,
						paginationSize);
				count = materialaddressService.countMaterialaddresss();
				break;
			case "Producer":
				entitySet = producerDAO.findAllProducers((pageNumber - 1) * paginationSize, paginationSize);
				count = producerService.countProducers();
				break;
			case "Supplier":
				entitySet = supplierDAO.findAllSuppliers((pageNumber - 1) * paginationSize, paginationSize);
				count = supplierService.countSuppliers();
				break;
			}
		} else {
			switch (selectItem) {
			// 外键

			// 被引用键
			case "Additiveaddress":
				entitySet = additiveaddressDAO.findAdditiveaddressByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = additiveaddressService.countAdditiveaddresssByName(queryword);
				break;
			case "Address":
				entitySet = addressDAO.findAddressByNameContaining(queryword, (pageNumber - 1) * paginationSize,
						paginationSize);
				count = addressService.countAddresssByName(queryword);
				break;
			case "Detectdepartment":
				entitySet = detectdepartmentDAO.findDetectdepartmentByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = detectdepartmentService.countDetectdepartmentsByName(queryword);
				break;
			case "Event":
				entitySet = eventDAO.findEventByNameContaining(queryword, (pageNumber - 1) * paginationSize,
						paginationSize);
				count = eventService.countEventsByName(queryword);
				break;
			case "Foodsaleaddress":
				entitySet = foodsaleaddressDAO.findFoodsaleaddressByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = foodsaleaddressService.countFoodsaleaddresssByName(queryword);
				break;
			case "Materialaddress":
				entitySet = materialaddressDAO.findMaterialaddressByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = materialaddressService.countMaterialaddresssByName(queryword);
				break;
			case "Producer":
				entitySet = producerDAO.findProducerByNameContaining(queryword, (pageNumber - 1) * paginationSize,
						paginationSize);
				count = producerService.countProducersByName(queryword);
				break;
			case "Supplier":
				entitySet = supplierDAO.findSupplierByNameContaining(queryword, (pageNumber - 1) * paginationSize,
						paginationSize);
				count = supplierService.countSuppliersByName(queryword);
				break;
			}
		}
		/**
		 * 将Json数据转换成jsonArray形式，需要通过pson方式进行转换，然后再转换成JsonArray。因为存在懒加载
		 * 没有直接使用CmiPsonService中的fromJson方法是因为，fromJson方法会将int类型的直接转换成double类型的数据，这个问题还没有解决
		 */
		// 没有测试，深度需要多少？？？
		String jsonData = cmiPsonService.toJsonString(entitySet, 1);
		JSONArray jsonArray = new JSONArray();
		jsonArray = (JSONArray) JSON.parse((String) jsonData);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("list", jsonArray);
		jsonObject.put("paginationSize", paginationSize);
		jsonObject.put("pageNumber", pageNumber);
		jsonObject.put("totalRow", count);
		jsonObject.put("totalPaginations",
				count % paginationSize == 0 ? count / paginationSize : count / paginationSize + 1);
		return jsonObject;
	}
}
